﻿Imports System.Data.OleDb
Imports BUS
Imports DTO
Public Class frmChiTietPhieuThue
    Private ctptBus As New ChiTietPhieuThueBUS()

    Private Sub frmChiTietPhieuThue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()
        '-----------
        DuaDuLieuVaoComboBoxPhieuThue()
        DuaDuLieuVaoComboBoxKhachHang()
        Me.btnLuu.Visible = False
        Me.btnDongy.Visible = False
    End Sub
    Protected Function ConnectionData() As OleDbConnection
        Dim strCn As String
        strCn = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = QLKS.mdb"
        Dim cn As New OleDbConnection(strCn)
        cn.Open()
        Return cn
    End Function
    Private Sub LoadDuLieu()
        Dim cn As OleDbConnection
        cn = Me.ConnectionData()
        Dim strSQL As String
        strSQL = "select ctpt.MaChiTietPhieuThue, TenKH, kh.MaKhachHang, MaPhieuThue "
        strSQL += "from "
        strSQL += " ChiTietPhieuThue ctpt, KhachHang kh"
        strSQL += " where "
        strSQL += " ctpt.makhachhang = kh.makhachhang"

        Dim da As OleDbDataAdapter
        da = New OleDbDataAdapter(strSQL, cn)
        Dim cmb As New OleDbCommandBuilder(da)

        Dim DataSet1 As New DataSet()
        da.Fill(DataSet1, "TestDB")
        dtgvDanhSachPhieuThue.DataSource = DataSet1.Tables("TestDB")

    End Sub
    Private Sub DuaDuLieuVaoComboBoxKhachHang()
        ' nhap nhap chon csdl
        Dim cn As OleDbConnection
        cn = Me.ConnectionData()
        Dim strSQL As String
        strSQL = "select * from KhachHang"
        Dim da As New OleDbDataAdapter
        da = New OleDbDataAdapter(strSQL, cn)
        Dim ds As New DataSet
        da.Fill(ds, "KhachHang")
        With cboMakh
            .DataSource = ds.Tables("KhachHang")
            .ValueMember = "MAKHACHHANG"
            .DisplayMember = "TENKH"
        End With

        'Me.cboMapt.DataSource = ds.Tables(0).DefaultView

        ' Me.cboMapt.DisplayMember = "MAPHIEUTHUE"
        ' Me.cboMapt.ValueMember = "MAPHIEUTHUE"


    End Sub
    Private Sub DuaDuLieuVaoComboBoxPhieuThue()
        ' nhap nhap chon csdl
        Dim cn As OleDbConnection
        cn = Me.ConnectionData()
        Dim strSQL As String
        strSQL = "select * from PhieuThue"
        Dim da As New OleDbDataAdapter
        da = New OleDbDataAdapter(strSQL, cn)
        Dim ds As New DataSet
        da.Fill(ds, "PhieuThue")
        With cboMapt
            .DataSource = ds.Tables("PhieuThue")
            .ValueMember = "MAPHIEUTHUE"
            .DisplayMember = "MAPHIEUTHUE"
        End With

        'Me.cboMapt.DataSource = ds.Tables(0).DefaultView

        ' Me.cboMapt.DisplayMember = "MAPHIEUTHUE"
        ' Me.cboMapt.ValueMember = "MAPHIEUTHUE"


    End Sub

    Private Sub dtgvDanhSachPhieuThue_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgvDanhSachPhieuThue.SelectionChanged
        Try
            Me.txtMactpt.Text = Me.dtgvDanhSachPhieuThue.CurrentRow.Cells(0).Value
            '  Me.cboTenkh.SelectedValue = Me.dtgvDanhSachPhieuThue.CurrentRow.Cells(1).Value
            Me.cboMapt.Text = Me.dtgvDanhSachPhieuThue.CurrentRow.Cells(3).Value.ToString
            Me.cboMakh.SelectedValue = Me.dtgvDanhSachPhieuThue.CurrentRow.Cells(2).Value

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim str As String
        str = MsgBox("Bắt đầu thêm mới dữ liệu - Mời bạn nhấp chuột nhập liệu", MsgBoxStyle.YesNo, "Chú ý! chú ý!")
        If str = "6" Then

            Me.txtMactpt.Enabled = False
            Me.btnLuu.Visible = True
            Me.btnThem.Visible = False

        End If
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        Try
            Dim dt As New DataTable
            dt = ctptBus.LayBang()
            Dim dr As DataRow
            dr = dt.NewRow()
            dr("MaPhieuThue") = cboMapt.SelectedValue
            dr("MaKhachHang") = Me.cboMakh.SelectedValue

            ctptBus.ThemChiTietPhieuThueBUS(dr)
            MessageBox.Show("Thêm thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txtMactpt.Enabled = True
            Me.btnLuu.Visible = False
            Me.btnThem.Visible = True
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim str As String
        str = MsgBox("Xóa dữ liệu ?", MsgBoxStyle.YesNo, "Chú ý! chú ý!")
        If str = "6" Then
            ctptBus.XoaChiTietPhieuThueBUS(txtMactpt.Text)
            MessageBox.Show("Xóa thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            LoadDuLieu()
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim str As String
        str = MsgBox("Sửa dữ liệu?", MsgBoxStyle.YesNo, "Chú ý! chú ý!")
        If str = "6" Then

            Me.txtMactpt.Enabled = False
            Me.btnDongy.Visible = True
            Me.btnSua.Visible = False

        End If
    End Sub

    Private Sub btnDongy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDongy.Click
        Try
            Dim dt As New DataTable
            dt = ctptBus.LayBang()
            Dim dr As DataRow
            dr = dt.NewRow()
            dr("MaPhieuThue") = Me.cboMapt.SelectedValue
            dr("MaKhachHang") = cboMakh.SelectedValue
            dr("MaChiTietPhieuThue") = txtMactpt.Text


            ctptBus.SuaChiTietPhieuThueBUS(dr)
            Me.txtMactpt.Enabled = True
            Me.btnDongy.Visible = False
            Me.btnSua.Visible = True
            MessageBox.Show("Sửa thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub
End Class